package com.bdqn.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.dao.GarouselMapper;
import com.bdqn.bookshop.entity.Garousel;
import com.bdqn.bookshop.service.GarouselService;
import com.bdqn.bookshop.utils.DataGridViewResult;
import com.bdqn.bookshop.utils.FileResult;
import com.bdqn.bookshop.utils.FileUtil;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.vo.GarouselVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 16:00
 **/
@Service
@Transactional
public class GarouselServiceImpl extends ServiceImpl<GarouselMapper, Garousel> implements GarouselService {

    @Override
    public Result addGarousel(MultipartFile file, Garousel garousel, HttpServletRequest request) {
        if (file!=null){
            String newFileName = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            FileResult fr = FileUtil.saveFileToPath(request,file, BookConstants.GAROUSEL_SAVE_PATH,BookConstants.GAROUSEL_MAPPING_URL,newFileName);
            if (fr!=null){
                garousel.setImageUrl(fr.getHttpUrl());
            }
        }
        garousel.setCreateTime(new Date());
        garousel.setChangeTime(new Date());
        if(baseMapper.insert(garousel)>0){
            return Result.createSuccess("添加成功");
        }
        return Result.createError("添加失败");
    }

    @Override
    public Result updateGarousel(MultipartFile file, Garousel garousel, HttpServletRequest request) {
        if (file!=null){
            String fileName = garousel.getImageUrl().substring(garousel.getImageUrl().lastIndexOf('/')+1);
            FileUtil.deleteFile(request,fileName,BookConstants.GAROUSEL_SAVE_PATH);
            String newFileName = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            FileResult fr = FileUtil.saveFileToPath(request,file, BookConstants.GAROUSEL_SAVE_PATH,BookConstants.GAROUSEL_MAPPING_URL,newFileName);
            if (fr!=null){
                garousel.setImageUrl(fr.getHttpUrl());
            }
        }
        garousel.setChangeTime(new Date());
        if(baseMapper.updateById(garousel)>0){
            return Result.createSuccess("修改成功");
        }
        return Result.createError("修改失败");
    }

    @Override
    public Result deleteById(Integer id,String imageUrl,HttpServletRequest request) {
        if(baseMapper.deleteById(id)>0) {
            String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
            FileUtil.deleteFile(request, fileName, BookConstants.GAROUSEL_SAVE_PATH);
            return Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public Result batchDelete(String ids, String images, HttpServletRequest request) {
        String [] idArr = ids.split(",");
        String [] imageArr = images.split(",");
        if(baseMapper.deleteBatchIds(Arrays.asList(idArr))>0){
            for (String s : imageArr) {
                String fileName = s.substring(s.lastIndexOf('/') + 1);
                FileUtil.deleteFile(request, fileName, BookConstants.GAROUSEL_SAVE_PATH);
            }
            return Result.createSuccess("删除成功");
        }
        return Result.createSuccess("删除失败");
    }

    @Override
    public DataGridViewResult garouselList(GarouselVo garouselVo) {
        IPage<Garousel> page = new Page<Garousel>(garouselVo.getPage(),garouselVo.getLimit());
        QueryWrapper<Garousel> queryWrapper = new QueryWrapper<Garousel>();
        queryWrapper.like(StringUtils.isNotBlank(garouselVo.getTitle()),"title",garouselVo.getTitle());
        queryWrapper.ge(garouselVo.getStartTime()!=null,"create_time",garouselVo.getStartTime());
        queryWrapper.le(garouselVo.getEndTime()!=null,"create_time",garouselVo.getEndTime());
        baseMapper.selectPage(page,queryWrapper);
        return new DataGridViewResult(page.getTotal(),page.getRecords());
    }

    @Override
    public Result isSortCount(Integer id, Integer sort) {
        QueryWrapper<Garousel> queryWrapper = new QueryWrapper<Garousel>();
        queryWrapper.eq(sort!=null,"sort",sort);
        Garousel garousel = baseMapper.selectOne(queryWrapper);
        if(id!=null){
            if (garousel.getId()==id){
                return Result.createSuccess("可使用");
            }
        }
        if (garousel!=null){
            return Result.createError("排序存在重复");
        }
        return Result.createSuccess("可使用");
    }

    @Override
    public List<Garousel> getGarousel() {
        IPage<Garousel> page = new Page<>(1,BookConstants.GAROUSEL_LIMIT);
        QueryWrapper<Garousel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        baseMapper.selectPage(page,queryWrapper);
        return page.getRecords();
    }
}
